package com.example.hp.serviceproject;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class ThreadTestActivity extends AppCompatActivity {

   private TextView tv;
    private Handler handler;
    private int value = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thread_test);
        tv = (TextView) findViewById(R.id.show);
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                int value = msg.getData().getInt("value");
                tv.setText(value+"");

            }
        };
    }

    public void cal(View view) {
            new Thread(){
                @Override
                public void run() {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Message msg = new Message();
                    Bundle bundle = new Bundle();
                    bundle.putInt("value",++value);
                    msg.setData(bundle);
                    handler.sendMessage(msg);
                }
            }.start();
    }
}
